<?php

namespace app\controller\property\piles;

use think\App;
use app\controller\property\Base;
use app\common\repositories\piles\PilesFaultyLogRepository;

class PilesFaultyLog extends Base
{
    protected $repository;

    public function __construct(App $app, PilesFaultyLogRepository $repository)
    {
        parent::__construct($app);

        $this->repository = $repository;
    }

    public function list()
    {
        if ($this->request->isAjax()) {
            $where = $this->request->param([
                'keywords' => '',
                'add_time' => '',
                'piles_id' => ''
            ]);
            [$page, $limit] = $this->getPage();
            $res = $this->repository->getpropertyList($where, $page, $limit, $this->request->propertyId);
            return json()->data(['code' => 0, 'data' => $res['list'], 'count' => $res['count']]);
        }
        return $this->fetch('piles/faultyLog/list', [
            'handleAuth' => property_auth('propertyPilesFaultyLogHandle'),
            'delAuth' => property_auth('propertyPilesFaultyLogDel'),
        ]);
    }


    public function handle()
    {
        $id = (int)$this->request->param('id');
        if (!$id) {
            return $this->error('参数错误');
        }
        $info = $this->repository->get($id);
        if (!$info) {
            return $this->error('信息错误');
        }
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'status' => '',
                'handle_remark' => '',
            ]);
            $param['handle_time'] = date('Y-m-d H:i:s');
            try {
                $res = $this->repository->handleInfo($info, $param, $this->request->userId);
                if ($res) {
                    return $this->success('操作成功');
                } else {
                    return $this->error('操作失败');
                }
            } catch (\Exception $e) {
                exception_log('操作失败', $e);
                return $this->error('网络错误' );
            }
        } else {
            return $this->fetch('piles/faultyLog/handle', [
                'info' => $info,
            ]);
        }
    }

    public function del()
    {
        $ids = (array)$this->request->param('ids');
        try {
            $data = $this->repository->batchDelete($ids);
            if ($data) {
                return $this->success('删除成功');
            } else {
                return $this->error('删除失败');
            }
        } catch (\Exception $e) {
            return $this->error('网络失败');
        }
    }

}